package controllers;

import models.Board;
import play.Logger;
import play.mvc.Controller;

import java.util.Random;

public class Application extends Controller {

    static Board myBoard = null;

    public static void index()
    {
        myBoard = new Board();
        render();
    }

    public static void playerMove(int playerMove)
    {
        //checka hvort game ended áður en player move er fært inn
        if (!gameEnded() && myBoard.getMark(playerMove) == '-')
        {
            myBoard.setBoxes(playerMove, 'O');

            //checka hvort game ended áður en tölva gerir
            if ( !gameEnded() )
            {
                myBoard.setBoxes(getComputerMove(), 'X');
            }
        }
        renderJSON( myBoard.getBoxes() ); //skila nýrri stöðu til baka
    }

    public static void startNewGame()
    {
        myBoard.initBoard();
        renderJSON( myBoard.getBoxes() );
    }

    public static void showStatus()
    {
         renderJSON( myBoard.getBoxes() );
    }

    //my simple and stupid move algoritm
    private static int getComputerMove()
    {
        int n = -1;

        // X X -
        // X X -
        // X X -
        if(myBoard.getMark(0) == 'X' && myBoard.getMark(1) == 'X' && myBoard.getMark(2) == '-')
        {
            n = 2;
            return n;
        }
        else if(myBoard.getMark(3) == 'X' && myBoard.getMark(4) == 'X' && myBoard.getMark(5) == '-')
        {
            n = 5;
            return n;
        }
        else if(myBoard.getMark(6) == 'X' && myBoard.getMark(7) == 'X' && myBoard.getMark(8) == '-')
        {
            n = 8;
            return n;
        }

        // - X X
        // - X X
        // - X X
        else if(myBoard.getMark(1) == 'X' && myBoard.getMark(2) == 'X' && myBoard.getMark(0) == '-')
        {
            n = 1;
            return n;
        }
        else if(myBoard.getMark(4) == 'X' && myBoard.getMark(5) == 'X' && myBoard.getMark(3) == '-')
        {
            n = 3;
            return n;
        }
        else if(myBoard.getMark(7) == 'X' && myBoard.getMark(8) == 'X' && myBoard.getMark(6) == '-')
        {
            n = 6;
            return n;
        }

        // X - X
        // X - X
        // X - X
        else if(myBoard.getMark(0) == 'X' && myBoard.getMark(2) == 'X' && myBoard.getMark(1) == '-')
        {
            n = 1;
            return n;
        }
        else if(myBoard.getMark(3) == 'X' && myBoard.getMark(5) == 'X' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(6) == 'X' && myBoard.getMark(8) == 'X' && myBoard.getMark(7) == '-')
        {
            n = 7;
            return n;
        }

        // X X X
        // X X X
        // - - -
        else if(myBoard.getMark(0) == 'X' && myBoard.getMark(3) == 'X' && myBoard.getMark(6) == '-')
        {
            n = 6;
            return n;
        }
        else if(myBoard.getMark(1) == 'X' && myBoard.getMark(4) == 'X' && myBoard.getMark(7) == '-')
        {
            n = 7;
            return n;
        }
        else if(myBoard.getMark(2) == 'X' && myBoard.getMark(5) == 'X' && myBoard.getMark(8) == '-')
        {
            n = 8;
            return n;
        }

        // - - -
        // X X X
        // X X X
        else if(myBoard.getMark(3) == 'X' && myBoard.getMark(6) == 'X' && myBoard.getMark(0) == '-')
        {
            n = 0;
            return n;
        }
        else if(myBoard.getMark(4) == 'X' && myBoard.getMark(7) == 'X' && myBoard.getMark(1) == '-')
        {
            n = 1;
            return n;
        }
        else if(myBoard.getMark(5) == 'X' && myBoard.getMark(8) == 'X' && myBoard.getMark(2) == '-')
        {
            n = 2;
            return n;
        }

        // X X X
        // - - -
        // X X X
        else if(myBoard.getMark(0) == 'X' && myBoard.getMark(6) == 'X' && myBoard.getMark(3) == '-')
        {
            n = 3;
            return n;
        }
        else if(myBoard.getMark(1) == 'X' && myBoard.getMark(7) == 'X' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(2) == 'X' && myBoard.getMark(8) == 'X' && myBoard.getMark(5) == '-')
        {
            n = 5;
            return n;
        }

        // - X X
        // X - X
        // X X -
        else if(myBoard.getMark(4) == 'X' && myBoard.getMark(8) == 'X' && myBoard.getMark(0) == '-')
        {
            n = 0;
            return n;
        }
        else if(myBoard.getMark(0) == 'X' && myBoard.getMark(8) == 'X' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(0) == 'X' && myBoard.getMark(4) == 'X' && myBoard.getMark(8) == '-')
        {
            n = 8;
            return n;
        }

        // X X -
        // X - X
        // - X X
        else if(myBoard.getMark(4) == 'X' && myBoard.getMark(6) == 'X' && myBoard.getMark(6) == '-')
        {
            n = 6;
            return n;
        }
        else if(myBoard.getMark(6) == 'X' && myBoard.getMark(2) == 'X' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(6) == 'X' && myBoard.getMark(4) == 'X' && myBoard.getMark(2) == '-')
        {
            n = 2;
            return n;
        }

        // O O -
        // O O -
        // O O -
        else if(myBoard.getMark(0) == 'O' && myBoard.getMark(1) == 'O' && myBoard.getMark(2) == '-')
        {
            n = 2;
            return n;
        }
        else if(myBoard.getMark(3) == 'O' && myBoard.getMark(4) == 'O' && myBoard.getMark(5) == '-')
        {
            n = 5;
            return n;
        }
        else if(myBoard.getMark(6) == 'O' && myBoard.getMark(7) == 'O' && myBoard.getMark(8) == '-')
        {
            n = 8;
            return n;
        }

        // - O O
        // - O O
        // - O O
        else if(myBoard.getMark(1) == 'O' && myBoard.getMark(2) == 'O' && myBoard.getMark(0) == '-')
        {
            n = 0;
            return n;
        }
        else if(myBoard.getMark(4) == 'O' && myBoard.getMark(5) == 'O' && myBoard.getMark(3) == '-')
        {
            n = 3;
            return n;
        }
        else if(myBoard.getMark(7) == 'O' && myBoard.getMark(8) == 'O' && myBoard.getMark(6) == '-')
        {
            n = 6;
            return n;
        }

        // O - O
        // O - O
        // O - O
        else if(myBoard.getMark(0) == 'O' && myBoard.getMark(2) == 'O' && myBoard.getMark(1) == '-')
        {
            n = 1;
            return n;
        }
        else if(myBoard.getMark(3) == 'O' && myBoard.getMark(5) == 'O' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(6) == 'O' && myBoard.getMark(8) == 'O' && myBoard.getMark(7) == '-')
        {
            n = 7;
            return n;
        }

        // O O O
        // O O O
        // - - -
        else if(myBoard.getMark(0) == 'O' && myBoard.getMark(3) == 'O' && myBoard.getMark(6) == '-')
        {
            n = 6;
            return n;
        }
        else if(myBoard.getMark(1) == 'O' && myBoard.getMark(4) == 'O' && myBoard.getMark(7) == '-')
        {
            n = 7;
            return n;
        }
        else if(myBoard.getMark(2) == 'O' && myBoard.getMark(5) == 'O' && myBoard.getMark(8) == '-')
        {
            n = 8;
            return n;
        }

        // O O O
        // - - -
        // O O O
        else if(myBoard.getMark(0) == 'O' && myBoard.getMark(6) == 'O' && myBoard.getMark(3) == '-')
        {
            n = 3;
            return n;
        }
        else if(myBoard.getMark(1) == 'O' && myBoard.getMark(7) == 'O' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(2) == 'O' && myBoard.getMark(8) == 'O' && myBoard.getMark(5) == '-')
        {
            n = 5;
            return n;
        }

        // - - -
        // O O O
        // O O O
        else if(myBoard.getMark(3) == 'O' && myBoard.getMark(6) == 'O' && myBoard.getMark(0) == '-')
        {
            n = 0;
            return n;
        }
        else if(myBoard.getMark(4) == 'O' && myBoard.getMark(7) == 'O' && myBoard.getMark(1) == '-')
        {
            n = 1;
            return n;
        }
        else if(myBoard.getMark(5) == 'O' && myBoard.getMark(8) == 'O' && myBoard.getMark(2) == '-')
        {
            n = 2;
            return n;
        }

        // - O O
        // O - O
        // O O -
        else if(myBoard.getMark(0) == 'O' && myBoard.getMark(4) == 'O' && myBoard.getMark(8) == '-')
        {
            n = 8;
            return n;
        }
        else if(myBoard.getMark(0) == 'O' && myBoard.getMark(8) == 'O' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(4) == 'O' && myBoard.getMark(8) == 'O' && myBoard.getMark(0) == '-')
        {
            n = 0;
            return n;
        }

        // O O -
        // O - O
        // - O O
        else if(myBoard.getMark(4) == 'O' && myBoard.getMark(2) == 'O' && myBoard.getMark(6) == '-')
        {
            n = 6;
            return n;
        }
        else if(myBoard.getMark(6) == 'O' && myBoard.getMark(2) == 'O' && myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(6) == 'O' && myBoard.getMark(4) == 'O' && myBoard.getMark(2) == '-')
        {
            n = 2;
            return n;
        }


        else if(myBoard.getMark(0) == 'O' && myBoard.getMark(4) == 'X' && myBoard.getMark(8) == 'O')
        {
            n = 5;
            return n;
        }
        else if(myBoard.getMark(6) == 'O' && myBoard.getMark(4) == 'X' && myBoard.getMark(2) == 'O')
        {
            n = 3;
            return n;
        }

        else if(myBoard.getMark(4) == '-')
        {
            n = 4;
            return n;
        }
        else if(myBoard.getMark(0) == '-')
        {
            n = 0;
            return n;
        }

        else
        {
            Random generator = new Random();
            boolean generate = true;
            while (generate)
            {
                n = generator.nextInt(9); // gefur tölu, frá 0 til 8
                if ( myBoard.getMark(n) == '-' )
                    generate = false; //stop generation
            }
            Logger.info("Generated number: " + n);
            return n;
        }
    }

    private static boolean gameEnded()
    {
        if (myBoard.getMark(0) != '-')
        {
            //line 0 - 1 - 2
            if (myBoard.getMark(0) == myBoard.getMark(1) && myBoard.getMark(0) == myBoard.getMark(2))
                return true;

            //line 0 - 3 - 6
            if (myBoard.getMark(0) == myBoard.getMark(3) && myBoard.getMark(0) == myBoard.getMark(6))
                return true;
        }

        if (myBoard.getMark(8) != '-')
        {
            //line 8 - 5 - 2
            if (myBoard.getMark(8) == myBoard.getMark(5) && myBoard.getMark(8) == myBoard.getMark(2))
                return true;

            //line 8 - 7 - 6
            if (myBoard.getMark(8) == myBoard.getMark(7) && myBoard.getMark(8) == myBoard.getMark(6))
                return true;
        }

        if (myBoard.getMark(4) != '-')
        {
            //line from middle 1 - 4 - 7
            if (myBoard.getMark(4) == myBoard.getMark(1) && myBoard.getMark(4) == myBoard.getMark(7))
                return true;

            //line from middle 3 - 4 - 5
            if (myBoard.getMark(4) == myBoard.getMark(3) && myBoard.getMark(4) == myBoard.getMark(5))
                return true;

            //line from middle 6 - 4 - 2
            if (myBoard.getMark(4) == myBoard.getMark(6) && myBoard.getMark(4) == myBoard.getMark(2))
                return true;

            //line from middle 0 - 4 - 8
            if (myBoard.getMark(4) == myBoard.getMark(0) && myBoard.getMark(4) == myBoard.getMark(8))
                return true;
        }

        //Auðvitað á þessi að vera neðst ef þetta er síðasta svæðið og mylla
        if (myBoard.isFull()) //tie;
            return true;

        return false;
    }

}